package ua.edu.onu.myfiler;
import java.io.*;
import java.util.Scanner;
import java.util.zip.*;

public class MyFiler {

    public static String path;
    public static String zpath;
    public static String text;
    public static void GetPath(){
        //System.out.println("Введите полный пут к файлу");
        //Scanner scan=new Scanner(System.in);
        path="D:\\file.txt";//scan.nextLine();
        zpath=path.replace("txt", "zip");
        //System.out.println(zpath);
    }
    public static void readText(){
        System.out.println("Введите текст:");
        Scanner scan=new Scanner(System.in);
        text=scan.nextLine();
    }
    public static String readerFiles(String path) throws FileNotFoundException {

        StringBuilder sb = new StringBuilder();
        File f = new File(path);
        if (f.exists())
        try {
            BufferedReader in = new BufferedReader(new FileReader( f.getAbsoluteFile()));
            try {
                String s;
                while ((s = in.readLine()) != null) {
                    sb.append(s);
                    sb.append("\n");
                }
            } finally {
                in.close();
            }
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
        return sb.toString();
    }
    public static void writeIntoFiles(){
        File file = new File(path);

        try {
            PrintWriter out = new PrintWriter(file.getAbsoluteFile());
            try {
                out.print(text);
            } finally {
                out.close();
            }
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void appenderFiles() throws FileNotFoundException {
        File file = new File(path);
        System.out.println("Введите текст:");
        Scanner scan=new Scanner(System.in);
        String text1=readerFiles(path);
        text1+=scan.nextLine();
        try {
            PrintWriter out = new PrintWriter(file.getAbsoluteFile());
            try {
                out.print(text1);
            } finally {
                out.close();
            }
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void archive() throws IOException {
        ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(zpath));
            FileInputStream fis= new FileInputStream(path);
        {
            ZipEntry entry1=new ZipEntry(path);
            zout.putNextEntry(entry1);
            // считываем содержимое файла в массив byte
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            // добавляем содержимое к архиву
            zout.write(buffer);
            // закрываем текущую запись для новой записи
            zout.closeEntry();
        }

    }
    public static void dearchive(){

    }
    public static void cleanerFiles(){
        File file = new File(path);
        try {
            PrintWriter out = new PrintWriter(file.getAbsoluteFile());
            try {
                out.print("");
            } finally {
                out.close();
            }
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void creatorFiles() throws IOException {
        do {
            System.out.println("Введите путь к файлу ии его имя:");
            Scanner scan = new Scanner(System.in);
            String path1 = scan.nextLine();
            File file = new File(path1);
            if (file.exists()) {
                System.out.println("Файл существует!");
            } else {file.createNewFile();break;}
        }while (true);
    }
    public static void sizerFiles(){
        File file = new File(path);
        System.out.println("Файл весит " + file.length() + " байт");
    }
    public static void managerFiles(){

    }
    public static void main(String[] args) throws IOException {
        GetPath();
        archive();
        /*System.out.println(readerFiles(path));
        readText();
        writeIntoFiles();
        System.out.println(readerFiles(path));
        appenderFiles();
        System.out.println(readerFiles(path));*/
    }
}
